package com.ssm.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.ssm.base.controller.BaseController;
import com.ssm.base.model.Congiguration;
import com.ssm.base.model.Page;
import com.ssm.domain.Goods;
import com.ssm.service.IGoodsService;
import com.ssm.util.mybatis.ExampleUtil;
import com.ssm.util.mybatis.FileUtil;

import tk.mybatis.mapper.entity.Config;

import com.ssm.util.mybatis.ExampleType.Operation;

/**
*
*suae  2016骞�7鏈�6鏃�
*@return
*/
@Controller
@RequestMapping("goods")
public class GoodsController extends BaseController {

	@Autowired
	private IGoodsService goodsService;
	
	/**
	 * 鏌ヨ鍟嗗搧鍒楄〃
	 * 
	 * @author suae
	 * @Date  2016骞�7鏈�6鏃�20:34
	 * 
	 * @param model
	 * @param Goods
	 * @param page
	 * @return
	 */
	@RequestMapping("query")
	public @ResponseBody PageInfo<Goods> query(Model model, Goods goods, Page page) {
		Object example = ExampleUtil.getInstance().addParam("title",goods.getTitle(), Operation.LIKE)//////// 宸ュ叿閲岄潰鏈夋敞閲�
				.addOrder("id")////// 鎺掑簭 鑷繁鎵嬪啓鍒嗙粍鐨勬椂鍊欏垎缁勪篃鑳界敤
				.getExample();
		PageInfo<Goods> pageInfo = goodsService.selectByExample(example, page);
		return pageInfo;
	}
	
	
	@RequestMapping("addOne")
	public @ResponseBody int add(Model model, Goods goods, Page page) throws Exception {
		System.err.println("goods:"+goods.getMainPicture());
		goods.setCreatedAt(new Date());
		int pageInfo = goodsService.insertSelective(goods);
		return pageInfo;
	}
	
	

	@RequestMapping("upload")
	public @ResponseBody String upload(HttpServletRequest request,@RequestParam MultipartFile[] file
) throws Exception{
		String names = "" ;
		System.out.println("upload:"+file.toString());
		Congiguration.setConfig("file_path", "C:\\Users\\suae\\Desktop\\java\\Project\\ssm\\src\\main\\webapp\\upload\\temp");
		System.out.println(file.length);
		for(MultipartFile  f : file){
			
		File name = FileUtil.save1(Congiguration.getString("file_path"), f);
//			Icon icon = new Icon();
//			icon.setDetail("澶村儚");
//			icon.setName(name);
//			icon.setPath(name);
//			icon.setText("澶村儚");
//			icon.setType(0);
//			iconService.insert(icon);
		String fullPath = name.getAbsolutePath();
		String path = fullPath.substring(62);
		System.out.println("fullPath:"+fullPath+"path:"+path);

		names = names+","+path;
		}
		return names;
	}
	
	@RequestMapping("uploadOne")
	public @ResponseBody String uploadOne(HttpServletRequest request,@RequestParam MultipartFile file
) throws Exception{
		String names = "" ;
		System.out.println("upload:"+file.toString());
		Congiguration.setConfig("file_path", "C:\\Users\\suae\\Desktop\\java\\Project\\ssm\\src\\main\\webapp\\upload\\temp");
		System.out.println(file.getSize());
			
		File name = FileUtil.save1(Congiguration.getString("file_path"), file);

		String fullPath = name.getAbsolutePath();
		String path = fullPath.substring(62);
		System.out.println("fullPath:"+fullPath+"path:"+path);
       String pString = path.replace("\\", "/");

		return pString;
	}
	
	@RequestMapping("download")    
    public void downloadMuch(HttpServletRequest request,@RequestParam String[] filesName) throws IOException {    
    	for(String  fileName : filesName){
    		  new ModelAndView("redirect:/downloadOne?fileName="+fileName);
    }    
    }
	@RequestMapping("downloadOne")    
    public ResponseEntity<byte[]> download(HttpServletRequest request,@RequestParam String fileName) throws IOException {    
    	String path=Congiguration.getString("file_path")+fileName;  
        File file=new File(path);  
        HttpHeaders headers = new HttpHeaders();    

        headers.setContentDispositionFormData("attachment", fileName);   
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);   
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
                                          headers, HttpStatus.CREATED);    
    
    }
}
